package com.ay.smartpark.sdk.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.ay.smartpark.sdk.common.base.DownloadFileBean;
import com.ay.smartpark.sdk.common.base.DownloadFileResponse;
import com.ay.smartpark.sdk.common.base.UploadFileBean;
import com.ay.smartpark.sdk.common.base.UploadFileResponse;
import com.ay.smartpark.sdk.common.utils.DownloadFileUtils;
import com.ay.smartpark.sdk.common.utils.EncryptUtils;
import com.ay.smartpark.sdk.common.utils.UploadFileUtils;
import com.ay.smartpark.sdk.common.utils.YmlUtils;
import com.ay.smartpark.sdk.service.UploadAndDownloadFileService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class UploadAndDownloadFileServiceImpl implements UploadAndDownloadFileService {
    @Override
    public UploadFileResponse uploadFiles(String defaultWebProtocol, String defaultWebDomain, String fieldName, String tableName, String phpSessionId, String tmpFilePath, MultipartFile[] multipartFiles) {
        List<File> fileList = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            String fileName = multipartFile.getOriginalFilename();
            String suffix = fileName.substring(fileName.lastIndexOf("."));
            String newFilePath = tmpFilePath + fileName + suffix;
            File file = new File(newFilePath);
            try {
                boolean res = file.createNewFile();
                if (!res) {
                    return UploadFileResponse.fail("创建临时文件失败");
                }
                //multipartFile转化成File
                multipartFile.transferTo(file);
            } catch (IOException e) {
                return UploadFileResponse.fail(e.getMessage());
            }
            fileList.add(file);
        }
        UploadFileBean uploadFileBean = new UploadFileBean();
        uploadFileBean.setWebProtocol(YmlUtils.getWeb_Protocol(defaultWebProtocol));
        uploadFileBean.setWebDomain(YmlUtils.getWWW_Domain(defaultWebDomain));
        uploadFileBean.setTableId(tableName);
        uploadFileBean.setFieldId(fieldName);
        uploadFileBean.setRecordId("-1");
        uploadFileBean.setPhpSessionId(phpSessionId);
        uploadFileBean.setFiles(fileList);
        UploadFileResponse uploadFileResponse = UploadFileUtils.uploadFiles(uploadFileBean);
        //删除临时文件
        UploadFileUtils.deleteFile(fileList);
        return uploadFileResponse;
    }

    @Override
    public DownloadFileResponse downloadFile(String defaultWebProtocol, String defaultWebDomain, String fileNameFromDB, String recordId, String fieldName, String tableName, String phpSessionId) {

        JSONArray jsonArray = JSON.parseArray(fileNameFromDB);
        JSONArray attachEncrypt = new JSONArray();
        for (Object object : jsonArray) {
            String attachEncryptStr = EncryptUtils.attachEncrypt(object.toString());
            attachEncrypt.add(attachEncryptStr);
        }
        String fileName = attachEncrypt.getString(0);

        DownloadFileBean downloadFileBean = new DownloadFileBean();
        downloadFileBean.setWebProtocol(YmlUtils.getWeb_Protocol(defaultWebProtocol));
        downloadFileBean.setWebDomain(YmlUtils.getWWW_Domain(defaultWebDomain));
        downloadFileBean.setTableId(tableName);
        downloadFileBean.setFieldId(fieldName);
        downloadFileBean.setRecordId(recordId);
        //从数据中获取的文件名要进行加密
        downloadFileBean.setFileName(fileName);
        downloadFileBean.setPhpSessionId(phpSessionId);

        DownloadFileResponse downloadFileResponse = DownloadFileUtils.downloadFile(downloadFileBean);
        return downloadFileResponse;
    }

    @Override
    public String getDownloadFileUrl(String defaultWebProtocol, String defaultWebDomain, String fileNameFromDB, String recordId, String fieldName, String tableName, String phpSessionId) {
        JSONArray jsonArray = JSON.parseArray(fileNameFromDB);
        JSONArray attachEncrypt = new JSONArray();
        for (Object object : jsonArray) {
            String attachEncryptStr = EncryptUtils.attachEncrypt(object.toString());
            attachEncrypt.add(attachEncryptStr);
        }
        String fileName = attachEncrypt.getString(0);

        DownloadFileBean downloadFileBean = new DownloadFileBean();
        downloadFileBean.setWebProtocol(YmlUtils.getWeb_Protocol(defaultWebProtocol));
        downloadFileBean.setWebDomain(YmlUtils.getWWW_Domain(defaultWebDomain));
        downloadFileBean.setTableId(tableName);
        downloadFileBean.setFieldId(fieldName);
        downloadFileBean.setRecordId(recordId);
        //从数据中获取的文件名要进行加密
        downloadFileBean.setFileName(fileName);
        downloadFileBean.setPhpSessionId(phpSessionId);
        String downloadFileUrl = DownloadFileUtils.getDownloadFileUrl(downloadFileBean);
        return downloadFileUrl;
    }
}
